let G = require('./glb');
require('./libs/mytools');

cc.Class({
    extends: cc.Component,

    properties: {
        nd_ui: cc.Node,
        nd_board: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        G.gameMap = this;

        //--------------- editorText
        this.lstRowControl = [];
        for (let i = 1; i <= 9; ++i) {
            let nd_row = cc.instantiate(G.pfRes.pf_row);
            nd_row.setParent(this.nd_ui);
            nd_row.setPosition(265, 270 - (i - 1) * 64);
            let row = nd_row.getComponent('row');
            row.setRowIdx(i - 1);
            this.lstRowControl.push(row);
        }

        //----------------- gong
        this.lstGong = [];
        for (let i = 1; i <= 9; ++i) {
            let gong = cc.find(`gong_${i}`, this.nd_board).getComponent('gong');
            gong.setGongIdx(i - 1);
            this.lstGong.push(gong);
        }
    },

    start() {
        this.lstRowControl.forEach(row => {
            row.init();
        });

        if(window.eui){
            window.eui.init();
        }
    },


    // 获取整个棋盘的值,作为计算结果的输入
    // 返回一个二维数组
    getBoardData() {
        let map = [];
        for (let i = 0; i < 9; ++i) {
            let cells = this.getCellsAtRow(i);
            map[i] = cells.map(cell => {
                return cell.getValue();
            });
        }
        return map;
    },

    /**
     * 返回 行索引为idx 的 那一行cell
     * @param idx
     * @return {Array}
     */
    getCellsAtRow(idx) {
        let gongRowIdx = Math.floor(idx / 3);
        let rowInGong = idx % 3;
        let gongs = this.getGongsAtRow(gongRowIdx);
        let cells = [];
        gongs.forEach(gong => {
            let subCells = gong.getGongRowCells(rowInGong);
            cells = cells.concat(subCells);
        });
        return cells;
    },

    /**
     *
     * @param idx 3个宫为一行, idx表示这个行索引,范围为 [0,2];
     */
    getGongsAtRow(idx) {
        if (idx < 0 || idx >= 3) {
            throw new Error(`idx:${idx} 为 0 , 1, 2 之间`);
        }
        let start = idx * 3;
        return this.lstGong.slice(start, start + 3);
    },

    showTips(msg) {
        let nd_tip = cc.instantiate(G.pfRes.pf_tips);
        nd_tip.setParent(this.node);
        nd_tip.zIndex = 10000;
        let tips = nd_tip.getComponent('tips');
        tips.setMsg(msg);
    },

    // 计算结果
    btn_calClicked() {
        let map = this.getBoardData();

        // 至少填入17个数字
        // 否则不合法
        if(!this.isValidData(map)){
            this.showTips('至少输入17个有效数字');
            return;
        }

        // // 计算过程中加一个loading,防止输入
        // const sudoku = new Sudoku(map);
        // if (sudoku.solve()) {
        //     let len = sudoku.results.length;
        //     if (len > 1) {
        //         this.showTips('找到多个结果,只取第一个结果....');
        //     }
        //     let ret = sudoku.results[0];
        //     console.log('结果为:', ret);
        //
        //     // 填入结果,
        //     this.fillResult(ret);
        //
        // } else {
        //     this.showTips('计算结果失败');
        // }
    },

    isValidData(map){
        let count = 0;
        for(let i = 0; i < map.length;++i){
            for(let j =0; j < map[i].length;++j){
                if(map[i][j] > 0){
                    count++;
                }
            }
        }
        return count >= 17;
    },

    fillResult(ret){
        // 对每一行来说
        for(let i = 0; i < 9;++i){
            let cells = this.getCellsAtRow(i);
            cells.forEach((cell,colIdx)=>{
                cell.setValue(ret[i][colIdx]);
            });
        }
    },

    clean(){
        this.lstRowControl.forEach(row => {
            row.init();
        });
    },
});
